package edu.must.uh.util;

import java.util.ResourceBundle;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtil {    
  
    
    private static JedisPool jedisPool; 
    
    
    private static void initialPool() {
        
      //使用ResourceBundle类读取配置文件
        ResourceBundle resourceBundle = ResourceBundle.getBundle("uh");
        //拿到数据信息
        String host = resourceBundle.getString("redis.host");
        String auth = resourceBundle.getString("redis.password");
        int port = Integer.parseInt(resourceBundle.getString("redis.port"));
        int maxTotal = Integer.parseInt(resourceBundle.getString("redis.maxTotal"));
        int maxIdle = Integer.parseInt(resourceBundle.getString("redis.maxIdle"));
        //设置配置信息
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxTotal(maxTotal);      
        //初始化
        jedisPool = new JedisPool(jedisPoolConfig, host, port,10000,auth);
    }
    
    static  {
        initialPool();
    } 
    
    private static Jedis getJedis() {
        if(jedisPool == null ||jedisPool.isClosed()) {          
            initialPool();
        }        
        return jedisPool.getResource();
     }
    
    //释放jedis和jedisPool资源
    private static void releaseResource(final Jedis jedis){
        if (jedis != null){
            jedis.close();
        }
        if (jedisPool != null){
            jedisPool.close();
        }
     }
    
    //静态的方法
    public static String get( String key ){
        if(key == null) {
            return null;  
        }
        Jedis jedis =null;
        try{
            jedis= getJedis();
            String GET = getJedis().get(key); 
            return GET;
            
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("获取缓存失败 key:" + key);
        }finally{
            releaseResource(jedis);
        }
        return null;  
  }
    
    public static String get( String masterName,String key ){
        Jedis jedis =null;
        if(key == null) {
            return null;  
        }
        try{
            jedis= getJedis();
            String GET = jedis.hget(masterName, key); 
            return GET;
            
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("获取缓存失败 key:" + key);
        }finally{
            releaseResource(jedis);
        }
        return null;  
  }
    
    public static Long del( String key ){
        Jedis jedis =null;
        if(key == null) {
            return (long) 0;  
        }
        try{
            jedis=getJedis();
            Long GET = jedis.del(key); 
            return GET;            
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("del缓存失败 key:" + key);
        }finally{
            releaseResource(jedis);
        }
        return null;  
     }
    
    public static  Long del(String masterName,String key ){
        if(key == null) {
            return (long) 0;  
        }
        Jedis jedis =null;
        try{
            jedis=getJedis();
            Long GET = jedis.hdel(masterName, key); 
            return GET;            
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("del缓存失败 key:" + key);
        }finally{
            releaseResource(jedis);
        }
        return null;  
  }
    
       //set方法
        public static void set(String key,String values){
            
            Jedis jedis =null; 
            try{
                jedis= getJedis();               
               jedis.set(key,values);            
               
            }catch(Exception e){
                e.printStackTrace();
                System.out.println("获取缓存失败 key:" + key);
            }finally{
                releaseResource(jedis);
            }   
        } 
        
        public static void set(String masterName,String key,String values){
            Jedis jedis =null; 
            try{
                jedis= getJedis();                
               jedis.hset(masterName, key, values);            
               
            }catch(Exception e){
                e.printStackTrace();
                System.out.println("获取缓存失败 key:" + key);
            }finally{
                releaseResource(jedis);
            }   
        }

}
